package com.example.taobaounion.presenter.impl;

import androidx.annotation.NonNull;

import com.example.taobaounion.model.Api;
import com.example.taobaounion.model.domain.TicketParams;
import com.example.taobaounion.model.domain.TicketResult;
import com.example.taobaounion.presenter.ITicketPresenter;
import com.example.taobaounion.utils.LogUtils;
import com.example.taobaounion.utils.RetrofitUtils;
import com.example.taobaounion.utils.UrlUtils;
import com.example.taobaounion.view.callback.ITicketPagerCallBack;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class TicketPresenter implements ITicketPresenter {


    public static final String TAG = "TicketPresenter";
    private String mCover;
    private TicketResult mTicketResult;

    enum state {
        loading,
        success,
        error,
        none
    }

    private state currentState = state.none;

    private ITicketPagerCallBack mViewCallBack;

    @Override
    public void getTicket(String title, String url, String cover) {
        mCover = cover;
        url = UrlUtils.getTicketUrl(url);

        LogUtils.d(TAG, "url --->" + url);


        Retrofit retrofit = RetrofitUtils.getInstance().getRetrofit();

        Api api = retrofit.create(Api.class);

        TicketParams ticketParams = new TicketParams(url, title);

        Call<TicketResult> task = api.getTicket(ticketParams);

        task.enqueue(new Callback<TicketResult>() {


            @Override
            public void onResponse(@NonNull Call<TicketResult> call, @NonNull Response<TicketResult> response) {
                int code = response.code();
                LogUtils.d(TAG, "code --->" + code);

                if (code == HttpURLConnection.HTTP_OK) {
                    mTicketResult = response.body();

                    assert response.body() != null;
                    LogUtils.d(TAG, "result --->" + response.body().toString());

                    if (mViewCallBack != null) {
                        mViewCallBack.onTicketLoaded(cover, mTicketResult);
                    } else {
                        currentState = state.success;
                    }
                } else {
                    // failure
                    onLoadTicketFailure();
                }
            }

            @Override
            public void onFailure(@NonNull Call<TicketResult> call, @NonNull Throwable t) {
                onLoadTicketFailure();
            }
        });
    }

    private void onLoadTicketFailure() {
        if (mViewCallBack != null) {
            mViewCallBack.onNetworkError();
        } else {
            currentState = state.error;
        }
    }

    @Override
    public void registerViewCallback(ITicketPagerCallBack callback) {

        this.mViewCallBack = callback;

        switch (currentState) {
            case loading:
                callback.onLoading();
                break;
            case success:
                callback.onTicketLoaded(mCover, mTicketResult);
                break;
            case error:
                callback.onNetworkError();
                break;
            case none:
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + currentState);
        }


    }

    @Override
    public void unRegisterViewCallback(ITicketPagerCallBack callback) {
        mViewCallBack = null;
    }
}
